"""
@author: jimmy
@file: 13.剪刀石头布.py
@time: 2024/8/23 14:35
"""
# 玩家
#   属性 姓名，房间
#   方法  战斗
# 房间
#   属性  房间号 房间里的成员
#   方法  加入玩家

import random


def add_player(player):
    for key in Player.player_list:
        if player == key:
            print(f"{player}已存在，无法新建")
    player = Player(player)
    player.add_player_list(player)
    print(f"您已成功创建{player.name}玩家")


def add_room(room_id):
    for key in Room.room_list:
        if room_id == key:
            print(f"{room_id}已存在，无法新建")
    room_id = Room(room_id)
    room_id.add_room_list(room_id)
    print(f"已创建{room_id.room_id}房间")


class Player:
    player_list = {}

    def __init__(self, name):
        self.name = name
        self.room = None
        self.HP = 5
        self.choose = 0

    @classmethod
    def show_player_list(cls):
        print("当前玩家列表为")
        print(cls.player_list)

    @classmethod
    def add_player_list(cls,player):
        cls.player_list[player.name]=player
        print(f"{player.name}已加入玩家列表")

    def show_HP(self):
        print(f"当前血量为{self.HP}")

    def player_clear(self):
        if self.HP == 0:
            print(f"{self.name}的血量为0，玩家死亡，清空玩家信息")
            Player.player_list.pop(self)


    def Fight(self):
        self.choose = random.randint(1, 3)
        if self.choose == 1:
            put = "剪刀"
        if self.choose == 2:
            put = "石头"
        if self.choose == 3:
            put = "布"
        print(f"战斗开始,{self.name}选择出{put}")

    def choose_room(self, room):
        if self.room is None:
            self.room = room.room_id
            room.add_player(self.name)


class Room:
    room_list = {}
    def __init__(self, room_id):
        self.room_id = room_id
        self.room_member = []

    @classmethod
    def add_room_list(cls,room):
        cls.room_list[room.room_id] = room
        print(f"{room.room_id}已加入房间列表")

    @classmethod
    def show_room_list(cls):
        print("当前房间列表为")
        print(cls.room_list)

    def add_player(self, player):
        if player not in self.room_member:
            self.room_member.append(player)
            print("添加成功")
        else:
            print("玩家已存在，无法添加")

    # def start(self):
    #     if len(self.room_member) == 2:
    #         print("玩家数量足够，开始游戏")
    #     else:
    #         print("玩家数量不够，无法开始游戏")
    #         return

    def round(self, player1: Player, player2: Player):
        Flag = True
        while Flag:
            player1.Fight()
            player2.Fight()
            if player1.choose == player2.choose:
                print("平局")
            if player1.choose - player2.choose == 1 or player1.choose - player2.choose == -2:
                print(f"{player1.name}获胜")
                player2.HP -= 1
            if player2.choose - player1.choose == 1 or player2.choose - player1.choose == -2:
                print(f"{player2.name}获胜")
                player1.HP -= 1
            if player1.HP == 0:
                print(f"最终{player2.name}获胜，游戏结束")
                player1.player_clear()
                Flag = False
                self.room_member.clear()
            if player2.HP == 0:
                print(f"最终{player1.name}获胜，游戏结束")
                player2.player_clear()
                Flag = False
                self.room_member.clear()


if __name__ == '__main__':
    # p=Player()
    # r=Room()
    Flag = True
    while Flag:
        choose = int(
            input("请输入您的选择，1.创建玩家\n2.创建房间\n3.查看玩家列表\n4.查看房间列表\n5.开始战斗\n其它选择.退出\n"))
        if choose == 1:
            player1 = input("请输入要创建的玩家姓名\n")
            add_player(player1)
        elif choose == 2:
            room1 = input("请输入要创建的房间名\n")
            add_room(room1)
        elif choose == 3:
            Player.show_player_list()
        elif choose == 4:
            Room.show_room_list()
        elif choose == 5:
            while Flag:
                choose = int(input("1.选择玩家加入房间\n2.开始战斗\n3.退回上级列表\n"))
                if choose == 1:
                    count = 0
                    player2 = input("请输入您选择的玩家\n")
                    for key in Player.player_list:
                        if player2 == key:
                            print("该玩家存在")
                            p = Player.player_list[key]
                            count += 1
                            break
                    else :
                        print("该玩家不存在，请重新输入")
                    room2 = input("请输入您选择的房间\n")
                    for key in Room.room_list:
                        if room2 == key:
                            print("该房间存在")
                            r = Room.room_list[key]
                            count += 1
                            break
                    else :
                        print("该房间不存在，请重新输入")
                    if count == 2:
                        p.choose_room(r)
                        count = 0
                    else:
                        print("房间或玩家输入不正确，无法加入房间，请重新加入")
                if choose == 2:
                    room2 = input("请输入您选择开始战斗的房间\n")
                    for key in Room.room_list:
                        if room2 != key:
                            print("该房间不存在，请重新输入")
                            break
                        else :
                            print("该房间存在")
                            r = Room.room_list[key]
                    if len(r.room_member) == 2:
                        print("玩家数量足够，开始游戏")
                        r.round(Player.player_list[r.room_member[0]],Player.player_list[r.room_member[1]])
                    else:
                        print("玩家数量不够，无法开始游戏")
                        break

                if choose == 3:
                    break
        else:
            Flag = False
